import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Regest from '../components/regest.vue'
import Userpage from '../components/personal_center.vue'
import Mymessage from '../components/children/Mymessage.vue'
import Myorder from '../components/children/Myorder.vue'
import Mainpage from '../components/Mainpage.vue'
import store from '../store'
import Search from '../components/search.vue'
import Mypublish from '../components/children/Mypublish.vue'
import Myinfo from '../components/children/Myinfo.vue'
import Myfriend from "../components/children/Myfriend.vue"
import Myrequest from "../components/children/Myrequest.vue"
import Goodinfo from "../components/Goodinfo.vue"
import Requestinfo from "../components/Requestinfo.vue"
import Mycollect from "../components/children/Mycollect.vue"
import Backstage from "../components/Backstage.vue"
import AdminLogin from "../components/AdminLogin.vue"
import Mysell from "../components/children/Mysell.vue"


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
//防止重复跳转同一个路由
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/home' },
    { path: '/login', component: Login },
    { path: '/regest', component: Regest },
    { path: '/backstage', component: Backstage },
    { path: '/adminlogin', component: AdminLogin },
    {
      path: '/home', component: Home, redirect: '/mainpage',
      children: [
        { path: '/mainpage', component: Mainpage },
        {
          path: '/personal_center', component: Userpage, redirect: '/myinfo',
          children: [
            { path: '/myinfo', component: Myinfo },
            { path: "/mymessage", component: Mymessage },
            { path: '/myorder', component: Myorder },
            { path: '/mypublish', component: Mypublish },
            { path: '/myrequest', component: Myrequest },
            { path: '/myfriend', component: Myfriend },
            { path: '/mycollect', component: Mycollect },
            { path: '/mysell', component: Mysell }
          ]
        },
        { path: '/search', component: Search },
        { path: '/goodinfo', component: Goodinfo },
        { path: '/requestinfo', component: Requestinfo }
      ]
    },


  ]
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to将要访问的路径
  //from从哪个路径跳转来
  //next是一个函数,表示放行
  if (to.path === '/login') return next()
  if (to.path === '/regest') return next()
  if (to.path === '/home') return next()
  if (to.path === '/mainpage') return next()
  if (to.path === '/search') return next()
  if (to.path === '/goodinfo') return next()
  if (to.path === '/requestinfo') return next()
  if (to.path === '/adminlogin') return next()
  if (to.path === '/backstage') {
    if (store.state.isAdmin === true) {
      return next();
    }
    alert("您不是管理员!")
    return next("/mainpage")
  }
  if (store.state.isLogin === false) {
    alert("请先登录!")
    return next("/mainpage")
  }
  next()
})

export default router
